package base;

import equipment.EquipmentUtils;
import food.FoodManager;
import impsoft.bots.ColorBot;
import impsoft.bots.input.ChatCommand;
import impsoft.scripting.ibot.builtin.itemrec.IdentifiedItem;
import impsoft.scripting.ibot.builtin.itemrec.ItemSlot;
import impsoft.scripting.ibot.interfaces.AutoPaint;
import impsoft.scripting.ibot.interfaces.ChatListener;
import impsoft.scripting.ibot.interfaces.DepositBoxInRandom;
import impsoft.scripting.ibot.interfaces.HumanMouseListener;
import impsoft.scripting.ibot.interfaces.InventoryMonitor;
import impsoft.scripting.ibot.itemrec.ItemCondition;
import impsoft.scripting.types.ColorScript;
import impsoft.utils.general.Timer;
import inventory.InventoryUtils;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import locating.ObjectFinder;
import mouse.Mouse;
import mouse.MouseTrack;
import potions.PotionUtils;
import statistics.ExperienceHandler;
import statistics.LevelGoalHandler;
import tabs.TabHandler;
import teleporting.TeleportUtils;
import branding.Brand;
import breaking.LogoutHandler;

import combat.CombatUtils;

public class DreamScript extends ColorScript implements AutoPaint, ChatListener, InventoryMonitor, HumanMouseListener,
		DepositBoxInRandom, ScriptHandler {

	public static String name = "";
	public static String author = "";
	public static String description = "";
	public static double version = 1.00;
	public static String website = "";
	protected static Brand[] brandValues = new Brand[0];
	protected String logTag;

	protected DreamUtils utils = new DreamUtils(this);

	protected Timer timeRan = new Timer(0);
	public boolean paintMouseTrail = false;
	public List<MouseTrack> mouseTrackList = new ArrayList<MouseTrack>();
	private TabHandler tabHandler = new TabHandler(this);
	private LogoutHandler logoutHandler = new LogoutHandler(this);
	private PotionUtils potionUtils = new PotionUtils(this);
	private Mouse mouse = new Mouse(this);
	private InventoryUtils inventoryUtils = new InventoryUtils(this);
	private CombatUtils combatUtils = new CombatUtils(this);
	private FoodManager foodManager = new FoodManager(this);
	private ObjectFinder objectFinder = new ObjectFinder(this);
	private EquipmentUtils equipmentUtils = new EquipmentUtils(this);
	private LevelGoalHandler levelGoalHandler = new LevelGoalHandler(this);
	private TeleportUtils teleportUtils = new TeleportUtils(this);
	private ExperienceHandler experienceHandler = new ExperienceHandler();

	public DreamScript(ColorBot d) {
		super(d);
	}

	public void log(Object o) {
		if (logTag != null) {
			super.log(logTag + ": " + o == null ? "null" : o.toString());
		} else {
			super.log(o);
		}
	}
	
	public void setTag(String tag) {
		this.logTag = tag;
	}

	public Timer getTimeRan() {
		return timeRan;
	}

	public void rebrand() {
		int pid = getHandler().getProductID();
		for (Brand brand : brandValues) {
			if (brand.pid == pid) {
				name = brand.productName;
				author = brand.author;
				website = brand.website;
				break;
			}
		}
	}

	public void sleepUntilNotMoving() throws InterruptedException {
		Timer t = new Timer(1000);
		while (!isMoving() && !t.isUp()) {
			sleep(100);
		}

		int notMovingCount = 0;
		while (true) {
			sleep(100);
			if (isMoving()) {
				notMovingCount = 0;
			} else {
				notMovingCount++;
			}

			if (notMovingCount > 2) {
				break;
			}
		}
	}

	public void setFastRun(boolean on) throws InterruptedException {
		if (getSettingArray()[173] == 1 == on) {
			return;
		}

		int energy = theMiniMapIndicators.getEnergy();
		if (energy < 70 && on) {
			return;
		}

		theTabs.Options.setRun(on);
		sleep(1000);
	}

	public boolean waitForMenu() throws InterruptedException {
		Timer t = new Timer(1000);
		while (!theMenuFinder.isMenuUp() && t.isNotUp()) {
			sleep(100);
		}

		return theMenuFinder.isMenuUp();
	}

	public void sendMessage(String message) throws InterruptedException {
		enterCommand(ChatCommand.createCommand(this.getPriority(), Thread.currentThread(), message, random(1, 3)));
		SendKey(java.awt.event.KeyEvent.VK_ENTER);
	}

	@Override
	public boolean mouse(MouseEvent arg0) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void inventoryItemAppeared(IdentifiedItem arg0, ItemSlot arg1) throws InterruptedException {
		// TODO Auto-generated method stub

	}

	@Override
	public void chat(String arg0) throws InterruptedException {
		// TODO Auto-generated method stub

	}

	@Override
	public void paint(Graphics g) {
		g.dispose();
	}

	@Override
	public void script() throws InterruptedException, Exception {
		// TODO Auto-generated method stub

	}

	@Override
	public ItemCondition getItemsToDeposit() {
		// TODO Auto-generated method stub
		return null;
	}

	public DreamUtils getUtils() {
		return utils;
	}

	@Override
	public TabHandler getTabHandler() {
		return tabHandler;
	}

	@Override
	public LogoutHandler getLogoutHandler() {
		return logoutHandler;
	}

	@Override
	public Mouse getMouse() {
		return mouse;
	}

	@Override
	public InventoryUtils getInventoryUtils() {
		return inventoryUtils;
	}

	@Override
	public CombatUtils getCombatUtils() {
		return combatUtils;
	}

	@Override
	public FoodManager getFoodManager() {
		return foodManager;
	}

	@Override
	public ObjectFinder getObjectFinder() {
		return objectFinder;
	}

	@Override
	public EquipmentUtils getEquipmentUtils() {
		return equipmentUtils;
	}

	@Override
	public LevelGoalHandler getGoalHandler() {
		return levelGoalHandler;
	}

	@Override
	public ExperienceHandler getExperienceHandler() {
		return experienceHandler;
	}

	@Override
	public TeleportUtils getTeleportUtils() {
		return teleportUtils;
	}

	@Override
	public PotionUtils getPotionUtils() {
		return potionUtils;
	}
}
